# Replication files for:
# "Generalizability of Heterogeneous Treatment Effect Estimates Across Samples"
# Alexander Coppock, Thomas J. Leeper, and Kevin J. Mullinix
# Proceedings of the National Academy of Sciences, Forthcoming

rm(list = ls())

library(dplyr)
library(readr)
library(reshape2)
library(coefplot)

scatter_df <- read_rds("CLM_scatter_df.rds")

scatterplot <-
ggplot(scatter_df, aes(x = estimate_mt,
                       y = estimate_original)) +
  geom_hline(yintercept = 0, alpha = 1, color = "lightgray") +
  geom_vline(xintercept = 0, alpha = 1, color = "lightgray") +
  geom_abline(slope = 1, intercept = 0, alpha = 0.5, color = "lightgray") +
  geom_segment(aes(x = estimate_mt, xend = estimate_mt, y = conf.high_original, yend = conf.low_original, color = `Difference in CATES`), alpha = .3) +
  geom_segment(aes(x = conf.high_mt, xend = conf.low_mt, y = estimate_original, yend = estimate_original, color = `Difference in CATES`), alpha = .3) +
  geom_point(aes(color = `Difference in CATES`, shape = `Difference in CATES`), alpha = 1.0) +
  geom_rug() +
  coord_fixed(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  scale_color_manual(values = c("red", "black")) +
  scale_shape_manual(values = c(15, 16)) +
  xlab("Mechanical Turk Version Standardized Estimate") +
  ylab("Original Version Standardized Estimate") +
  theme_bw() +
  theme(legend.position = "bottom",
        strip.background = element_blank(),
        strip.text = element_text(size = 9),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

g1 <- scatterplot + facet_wrap( ~ group_label, ncol = 6, drop = FALSE, dir = "v")
g2 <- scatterplot + facet_wrap(~ study_label, labeller = label_wrap_gen(width = 15), ncol = 6)

ggsave("CLM_PNAS_FINAL_FIGURE1.pdf", g1, height = 6, width = 9, dpi = 600)
ggsave("CLM_PNAS_FINAL_FIGURE2.pdf", g2, height = 9, width = 8, dpi = 600)
